package com.xry.controller;

import com.xry.bean.User;
import com.xry.service.UserService;
import com.xry.util.Page;
import com.xry.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.UUID;


@RestController
@CrossOrigin//允许跨域
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping
    public Result add(@RequestBody User user){
        try{
            Object obj=userService.add(user);
            return Result.success("新增成功",obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @PutMapping
    public Result edit(@RequestBody User user){
        try{
            Object obj=userService.edit(user);
            return Result.success("编辑信息保存成功",obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj=userService.del(id);
        return Result.success("删除成功",obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id")Integer id){
        Object obj=userService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all( User user){
        Object obj=userService.all(user);
        return Result.success(obj);
    }
    @GetMapping("page")
    public Result page(Page page, User user){
        //查询一共有多少条数据
        //查询当前页要显示的数据
        //将当前页封装到page对象中
        userService.page(page,user);
        //返回page对象
        return Result.success(page);
    }

    @GetMapping("cgender")
    public Result countGender() {
        Map<String, Integer> data = userService.countGender();
        return Result.success(data);
    }

}
